package source;

import java.awt.*;
import java.util.*;
import source.view.*;

public class BaseGroup extends Base{
	private Base[] bases;

	public BaseGroup(Base[] newBases, PlayerStatus player){
		bases = new Base[newBases.length];

		for(int i = 0; i < newBases.length; i++){
			if(newBases[i] != null){
			bases[i] = newBases[i];
			if(newBases[i].getStructureType() == StructureType.BASEGROUP){
				Base[] insideOf = ((BaseGroup)newBases[i]).getBases();
				((BaseGroup)newBases[i]).destroy();
				newBases[i] = null;
				Base[] bases2 = new Base[i];
				System.arraycopy(bases, 0, bases2, 0, i);
				
				bases = new Base[newBases.length-1 + insideOf.length];
		
	
				System.arraycopy(bases2, 0, bases, 0, i);
				for(int j = 0; j < insideOf.length; j++,i++){
					bases[i] = insideOf[j];
				}
				//MainScreen.writeToConsole("i: " + i + "bases.length: " + bases.length);
			}	
			}
		}

		sType = StructureType.BASEGROUP;

		player.addBase(this);
		this.setPlayer(player);
	}

	public boolean beginBuilding(UnitType uType){
		building = uType;
		for(int i = 0; i < bases.length; i++){
			bases[i].beginBuilding(uType);
		}
		return true;
	}

	public Base[] getBases(){
		return bases;
	}

	public int getProductionRate(){
		int sum = 0;
		for(int i = 0; i < bases.length; i++){
			sum += bases[i].getPlanet().getManpower();
		}
		return sum;
	 }
	

	public int getCurrentHP(){
		int sum = 0;
		for(int i = 0; i < bases.length; i++){
			//MainScreen.writeToConsole("i in current hp is " + i);
			sum += bases[i].getCurrentHP();
		}
		return sum;
	}

	public int getMaxHP(){
		int sum = 0;
		for(int i = 0; i < bases.length; i++){
			sum += bases[i].getMaxHP();
		}
		return sum;
	}

	public int getViewDistance(){
		int curMax = 0;
		for(int i = 0; i < bases.length; i++){
			if(curMax < bases[i].getViewDistance())
				curMax = bases[i].getViewDistance();
		}
		return curMax;
	}	
	

	public int getAttackPower(){
		int sum = 0;
		for(int i = 0; i < bases.length; i++){
			sum += bases[i].getAttackPower();
		}
		return sum;
	}

	public int getDefensivePower(){
		int sum = 0;
		for(int i = 0; i < bases.length; i++){
			sum += bases[i].getDefensivePower();
		}
		return sum;
	}

	public UnitType beingBuilt(){
		return building;
	}

	public void healUnit(Unit u){
	}

	public boolean removeHealing(Unit u){
		return false;
	}

	private void healUnits(int manpower){
	}

	public boolean newTurn(){
		boolean donebuilding = true;
		for(int i = 0; i < bases.length; i++){
			bases[i].newTurn();
			if(bases[i].beingBuilt() != null)
				donebuilding = false;
		}
		if(donebuilding)
			building = null;
		return true;
	}

	public boolean destroy(){
	//	MainScreen.writeToConsole(getPlayer().getEnergy() + "");
		getPlayer().removeBase(this);
		return true;
	}
		
}		
